package com.solomon.travel.common.web.controller;

import java.util.HashMap;

import javax.annotation.Resource;
import javax.crypto.MacSpi;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.solomon.travel.common.base.controller.BaseController;
import com.solomon.travel.common.custom.po.HotelCustom;
import com.solomon.travel.common.custom.po.MotorcadeCustom;
import com.solomon.travel.common.custom.po.RestaurantCustom;
import com.solomon.travel.common.model.ApiResponse;
import com.solomon.travel.common.po.Guide;
import com.solomon.travel.common.po.Scenic;
import com.solomon.travel.common.po.Shop;
import com.solomon.travel.common.po.User;
import com.solomon.travel.common.service.GuideService;
import com.solomon.travel.common.service.HotelService;
import com.solomon.travel.common.service.MotorcadeService;
import com.solomon.travel.common.service.RestaurantService;
import com.solomon.travel.common.service.ScenicService;
import com.solomon.travel.common.service.ShopService;
import com.solomon.travel.common.service.UserService;
import com.solomon.travel.common.util.ConstantUtils;

@RestController
@RequestMapping("/web")
public class LoginController extends BaseController{
	
	@Resource
	private UserService userService;
	
	@Resource
	private HotelService hotelService;
	
	@Resource
	private GuideService guideService;
	
	@Resource
	private ScenicService scenicService;
	
	@Resource
	private MotorcadeService motorcadeService;
	
	@Resource
	private ShopService shopService;
	
	@RequestMapping("/login")
	public ApiResponse login(HttpServletRequest request, String username,String password) throws Exception{
		User user = userService.login(username, password);
		if (user == null) {
			return new ApiResponse(ConstantUtils.RESPONSE_CODE_USERNAME_OR_PASSWORD_WRONG, "用户名或密码错误");
		}
		if (user.getStatus() == 0) {
			return new ApiResponse(ConstantUtils.RESPONSE_CODE_PERMISSION_DENY, "该用户没有权限");
		}
		HashMap<String, Object> map = new HashMap<>();
		map.put("user", user);
		switch (user.getType()) {
		case ConstantUtils.ORGANIZE_TYPE_GOV:
			
			break;
		case ConstantUtils.ORGANIZE_TYPE_HOTEL:
			HotelCustom hotelCustom = hotelService.getHotelByUser(user.getObjectId());
			if (hotelCustom != null) {
				hotelCustom.setDiningRoom(hotelService.getDiningRoomByHotel(hotelCustom.getObjectId()));
				hotelCustom.setMettingRoom(hotelService.getMettingRoomByHotel(hotelCustom.getObjectId()));
				hotelCustom.setRoomPrices(hotelService.listHotelRoomPrice(hotelCustom.getObjectId()));
				hotelCustom.setProtocal(hotelService.getHotelProtocal(hotelCustom.getObjectId()));
			}
			map.put("hotel", hotelCustom);
			break;
		case ConstantUtils.ORGANIZE_TYPE_AGENCY:
			break;
		case ConstantUtils.ORGANIZE_TYPE_GUIDE:
			Guide guide = guideService.getByUser(user.getObjectId());
			map.put("guide", guide);
			break;
		case ConstantUtils.ORGANIZE_TYPE_MOTORCADE:
			MotorcadeCustom motorcadeCustom = motorcadeService.getByUser(user.getObjectId());
			map.put("motorcade", motorcadeCustom);
			break;
		case ConstantUtils.ORGANIZE_TYPE_RESTAURUANT:
			RestaurantCustom restaurantCustom = restaurantService.getRestCustomByUser(user.getObjectId());
			map.put("restaurant", restaurantCustom);
			break;
		case ConstantUtils.ORGANIZE_TYPE_SCENIC:
			Scenic scenic = scenicService.getByUser(user.getObjectId());
			map.put("scenic", scenic);
			break;
		case ConstantUtils.ORGANIZE_TYPE_SHOP:
			Shop shop = shopService.getbyUser(user.getObjectId());
			map.put("shop", shop);
			break;
		default:
			break;
		}
		request.getSession().setAttribute("user", user);
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK, map);
	}
	
	@RequestMapping("/logout")
	public ApiResponse logout(HttpServletRequest request) {
		request.getSession().invalidate();
		return defaultResponse();
	}
	
	@Resource
	private RestaurantService restaurantService;
}
